package com.seuic.hnapps.util;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;

public class Up_RundataUtils {
    public void packRequest(String make_type, String make_num, String moneys, String make_bz,
                            String time, String f11, String f39) throws IOException {
        MoneyUtil moneyUtil = new MoneyUtil();
//        String path = "/sdcard/Test/";
//        String strFile = path + "0001" + Constants.YANTAI_TERMNO + DateUtil.getCurrentTime(DateUtil.NOMAL_DATE) + ".txt";
//        String strcontent = "";
//
//        if (loadFromSDFile(strFile) == null) {
//
//        } else {
//            strcontent = loadFromSDFile(strFile);
//        }

        String content ="\r\n" +
                Constants.YANTAI_TERMNO + "^" + make_type + "^" + make_num + "^" +
                moneyUtil.packRequest(moneys) + "^" + make_bz + "^" + time + "^" + f11 + "^" + f39 + "^0001";
        String fname = "/sdcard/Test/" + "0001" + Constants.YANTAI_TERMNO + DateUtil.getCurrentTime(DateUtil.NOMAL_DATE) + ".txt";
        File f = new File(Environment.getExternalStorageDirectory().getPath() + fname);

        if (!f.exists()) {
            Log.d("TestFile", "Create the file:" + content);
            f.getParentFile().mkdirs();
            f.createNewFile();
        }
        RandomAccessFile raf = new RandomAccessFile(f, "rwd");
        raf.seek(f.length());
        raf.write(content.getBytes());
        raf.close();

    }

    private String loadFromSDFile(String fname) {
        fname = "/" + fname;
        String result = null;
        try {
            File f = new File(Environment.getExternalStorageDirectory().getPath() + fname);
            int length = (int) f.length();
            byte[] buff = new byte[length];
            FileInputStream fin = new FileInputStream(f);
            fin.read(buff);
            fin.close();
            result = new String(buff, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();

        }
        return result;
    }

}
